package com.sntk.organauthomanager.application.dispatch;

import com.alibaba.fastjson.JSON;
import com.sntk.organauthomanager.application.dto.OrganizationDTO;
import com.sntk.organauthomanager.application.dto.OrganizationVO;
import com.sntk.organauthomanager.application.dto.ResponseData;
import com.sntk.organauthomanager.infrastructure.autho.PassToken;
import com.sntk.organauthomanager.query.OrganizationQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName: OrganDispatch
 * @author: wgchao
 * @createTime: 2020/7/21 8:15 PM
 * @Description: TODO
 */
@RestController
@RequestMapping("/sinohis/osm/interior/organ")
@Api(value="OrganDispatch",tags={"机构信息"})
public class OrganDispatch {
    @Autowired
    private OrganizationQuery organizationQuery;

    @GetMapping("/organ/passtoken")
    @PassToken
    @ApiOperation(value="查询机构信息")
    public ResponseData<String> searchOrgan(String institutionNumber){
        if(StringUtils.isEmpty(institutionNumber)){
            return null;
        }
        OrganizationDTO organization = organizationQuery.searchOrgan(institutionNumber);
        if(organization == null){
            return null;
        }
        return ResponseData.success().setData(JSON.toJSONString(organization));
    }

    /**
     * 查询当前机构及其子机构信息
     * @param institutionNumber
     * @return
     */
    public String searchOrganAndChilds(String institutionNumber){

        if(StringUtils.isEmpty(institutionNumber)){
            return null;
        }
        List<OrganizationVO> organizationVOList =  organizationQuery.searchOrganChilds(institutionNumber);
        if(organizationVOList == null || organizationVOList.isEmpty()){
            return null;
        }
        return JSON.toJSONString(organizationVOList);
    }

    @GetMapping("/organ/byname")
    @PassToken
    @ApiOperation(value="根据名字查询机构列表")
    public ResponseData<String> queryOrganizationByName(String institutionName){
        if(StringUtils.isEmpty(institutionName)){
            return null;
        }
        List<OrganizationDTO>  dtos = organizationQuery.queryOrganizationByName(institutionName);
        if(CollectionUtils.isEmpty(dtos)){
            return null;
        }
        return ResponseData.success().setData(JSON.toJSONString(dtos));
    }


}
